package io.elegant.sessions.widgets

import android.content.Context
import android.net.Uri
import androidx.appcompat.widget.AppCompatButton
import io.elegant.Elegant
import io.elegant.ElegantApiGrpc
import io.elegant.widgets.FileUploaderWidget
import io.grpc.stub.StreamObserver
import kotlin.reflect.KClass

class WidgetFileUploaderSession(
    stub: ElegantApiGrpc.ElegantApiStub,
    context: Context
): BaseWidgetSession<AppCompatButton, FileUploaderWidget<AppCompatButton>>(stub, context) {
    override val clazz = AppCompatButton::class
    @Suppress("UNCHECKED_CAST")
    override val widgetClazz = FileUploaderWidget::class as KClass<FileUploaderWidget<AppCompatButton>>
    override val sendStreamObserver: StreamObserver<Elegant.Interaction> = stub.widgetFileUploader(receiveStreamObserver)
    private var fileSelectedListener: ((Uri?) -> Unit)? = null
    fun setOnFileSelectedListener(callback: (Uri?) -> Unit) {
        fileSelectedListener = callback
    }
    fun setFile(file: Uri?) = fileSelectedListener?.let { it(file) }
}